<?php

namespace Modules\Admin\Http\Controllers\Admin;

use App\Exceptions\BusinessException;
use App\Helpers\CodeResponse;
use App\Http\Controllers\Admin\ApiController;
use Illuminate\Support\Facades\Hash;
use Modules\Admin\Http\Requests\InfoRequest;
use Modules\Admin\Services\AdminService;

class InfoController extends ApiController
{
    public function info()
    {
        $this->admin->getPermissionsViaRoles();
        return $this->success($this->admin);
    }

    public function update(InfoRequest $request)
    {
        $admin = AdminService::getInstance()->Update($this->admin, $request->only('username', 'name', 'mobile'));
        return $this->success($admin);
    }

    public function updatePassword(InfoRequest $request)
    {
        if (!Hash::check($request->old_password, $this->admin->password)) {
            throw new BusinessException(CodeResponse::OLD_PASSWORD_FAIL);
        }
        $request->offsetSet('password', $request->get('new_password'));
        AdminService::getInstance()->Update($this->admin, $request->only(['password']));
        return $this->success();
    }
}
